<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

class IndexController extends Controller{

    //上传图片
    protected function upload(){
        if (request()->hasFile('pic') && request()->file('pic')->isValid()) {
            $disk = Storage::disk('qiniu');
            $photo = request()->file('pic');
            $realPath = $photo->getRealPath();//临时文件路径
            $name = hash_file('md5',$realPath);
            $time = date('Y-m-d');
            $fileName = $time.'/'.$name.'.'.$photo->getClientOriginalExtension();
            // 上传到七牛
            $bool = $disk->put($fileName, file_get_contents($realPath));
            // 判断是否上传成功
            if ($bool) {
                $path = $disk->getUrl($fileName);
                $return = ['path'=>$path];
                return $this->returnJson(0,'',$return);
            }
        }
        return $this->returnJson(1,'上传错误');
    }

}
